package com.aijk.ehm.service;

import com.aijk.base.entity.PagingRequest;
import com.aijk.commons.utils.Utils;
import com.aijk.ehm.dao.mybatis.EhmResidentMapper;
import com.aijk.ehm.model.EhmResident;
import com.aijk.ehm.model.EhmResidentExample;
import com.aijk.ehm.model.qo.EhmResidentQo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EhmResidentService {
    private static Logger logger = LoggerFactory.getLogger(EhmResidentService.class);

    @Autowired
    private EhmResidentMapper mapper;

    public EhmResidentExample getExample(EhmResidentQo qo) {
        EhmResidentExample example = new EhmResidentExample();
        EhmResidentExample.Criteria criteria = example.createCriteria();
        if (qo != null) {
            if (Utils.isNotEmpty(qo.getSortway())) example.setOrderByClause(qo.getSortway());
            if (Utils.isNotEmpty(qo.getStatus())) criteria.andStatusEqualTo(qo.getStatus());
            if (qo.getTitle()!=null && !qo.getTitle().equals("")) criteria.andTitleLike("%"+qo.getTitle()+"%");
            if (qo.getCompanyId()!=null && Utils.toInteger(qo.getCompanyId()) != 0) criteria.andCompanyIdEqualTo(qo.getCompanyId());
            if (qo.getWorkno()!=null && !qo.getWorkno().equals("")) criteria.andWorknoEqualTo(qo.getWorkno());
            if (qo.getSn()!=null && !qo.getSn().equals("")) criteria.andSnEqualTo(qo.getSn());
            if (Utils.isNotEmpty(qo.getJstatus())) criteria.andJstatusEqualTo(qo.getJstatus());
            if (Utils.isNotEmpty(qo.getAppStatus())) criteria.andAppStatusEqualTo(qo.getAppStatus());
            if (qo.getInCompanyId()!=null && qo.getInCompanyId().size() > 0) criteria.andCompanyIdIn(qo.getInCompanyId());
            if (qo.getInIds()!=null && qo.getInIds().size() > 0) criteria.andIdIn(qo.getInIds());
            if (Utils.isNotEmpty(qo.getStatus())) criteria.andStatusEqualTo(qo.getStatus());
            if (Utils.isNotEmpty(qo.getNjstatus())) criteria.andJstatusNotEqualTo(qo.getNjstatus());
            if (qo.getDeptId()!=null && Utils.toInteger(qo.getDeptId()) != 0) criteria.andDeptIdEqualTo(qo.getDeptId());
            if (qo.getId()!=null && Utils.toInteger(qo.getId()) != 0) criteria.andIdEqualTo(qo.getId());
            if (qo.getUid()!=null && Utils.toInteger(qo.getUid()) != 0) criteria.andUidEqualTo(qo.getUid());
        }
        return example;
    }

    public int count(EhmResidentQo qo) {
        return mapper.countByExample(getExample(qo));
    }

    public int save(EhmResident bean) {
        return mapper.insertSelective(bean);
    }

    public int delete(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    public int update(EhmResident bean) {
        return mapper.updateByPrimaryKeySelective(bean);
    }

    public int updateDeptName(EhmResident bean ,EhmResidentQo qo ) {
        return mapper.updateByExampleSelective(bean ,getExample(qo) );
    }

    public EhmResident findOne(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<EhmResident> find(EhmResidentQo qo) {
        return mapper.selectByExample(getExample(qo));
    }

    public PageInfo<EhmResident> page(PagingRequest pagingRequest, EhmResidentQo qo) {
        PageHelper.startPage(pagingRequest.getPageNum() , pagingRequest.getPageSize());
        return new PageInfo<EhmResident>(mapper.selectByExample(getExample(qo)));
    }
}